package org.pvt.gui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.*;
import android.widget.ListAdapter;
import android.widget.ListView;
import org.jetbrains.annotations.NotNull;
import org.pvt.R;
import org.pvt.model.Models;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

/**
 * @author Pavel Talanov
 */
public class UIUtils {
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    @NotNull
    public static String floatToText(float amount, boolean canBeNegative) {
        if (amount < 0 && (!canBeNegative)) {
            return "0.0";
        }
        DecimalFormat df = decimalFormat();
        return df.format(amount);
    }

    private static DecimalFormat decimalFormat() {
        DecimalFormat df = new DecimalFormat();
        df.setMaximumIntegerDigits(5);
        df.setMinimumFractionDigits(0);
        df.setMaximumFractionDigits(2);
        df.setNegativePrefix("-");
        df.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
        return df;
    }

    public static float textToFloat(String totalText) {
        try {
            return decimalFormat().parse(totalText).floatValue();
        } catch (ParseException e) {
            return 0.0f;
        }
    }

    public static void setUpMainMenu(Menu menu, @NotNull Activity activity) {
        MenuInflater menuInflater = activity.getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
    }

    static void onMainMenuOptionSelected(MenuItem item, @NotNull Activity activity) {
        if (item.getItemId() == R.id.menuAbout) {
            showAboutDialog(activity);
        } else if (item.getItemId() == R.id.menuExit) {
            exitApp(activity);
        }
    }

    private static void showAboutDialog(@NotNull Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.menuAbout);
        builder.setMessage(R.string.menuAboutText);
        builder.setPositiveButton(R.string.dialogOk, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
            }
        });
        builder.show();
    }

    private static void exitApp(@NotNull Activity activity) {
        Intent i = new Intent();
        i.setAction(Intent.ACTION_MAIN);
        i.addCategory(Intent.CATEGORY_HOME);
        activity.startActivity(i);
        activity.finish();
    }

    public static void startActivityForEvent(Class<? extends Activity> activityClass, long eventId, @NotNull Activity parent) {
        Intent intent = new Intent(parent, activityClass);
        intent.putExtra(Models.Event.getExtrasName(), eventId);
        parent.startActivity(intent);
    }
}
